root = @root@

util_dir = $(root)/util

# gnu-style triple identifying the build machine;
# this is a dynamic value
# BUILD := $(shell $(root)/util/config.guess)

build_obj_dir = $(BUILD)
build_obj_dir_stamp = $(build_obj_dir)/stamp

# gnu-style triple identifying the host machine
HOST = @host@
# canonical triple
CANONICAL_HOST = @canonical_host@
HOST_ARCH = @host_arch@
HOST_OS = @host_os@
HOST_FILE_FORMAT = @host_file_format@

# same for the build machine
BUILD = @build@
CANONICAL_BUILD = @canonical_build@
BUILD_ARCH = @build_arch@
BUILD_OS = @build_os@

# front-end for this version of executor;
# currently one of `nextstep', `x' or `dos'
FRONT_END = @front_end@

# the location of various component directories
SRC_DIR = $(root)/src

FRONT_END_DIR = $(root)/src/config/front-ends/@front_end@
SOUND_DIR = $(root)/src/config/sound/@sound@

HOST_ARCH_DIR = $(root)/src/config/arch/$(HOST_ARCH)
HOST_OS_DIR = $(root)/src/config/os/$(HOST_OS)

BUILD_ARCH_DIR = $(root)/src/config/arch/$(BUILD_ARCH)
BUILD_OS_DIR = $(root)/src/config/os/$(BUILD_OS)

SYN68K_LIB_DIR = $(root)/syn68k/lib/@syn68k_host@
INCL_DIR = $(SRC_DIR)/include

# ahh, should BUILD_{OS, ARCH}_DIR be added to the VPATH?
VPATH = $(SRC_DIR):$(HOST_ARCH_DIR):$(HOST_OS_DIR):$(FRONT_END_DIR):$(SOUND_DIR)

# these are (possibly) cross-tools that run on the host
# and produce code for the host
# these names are host-independant
HOST_CC = splode    
HOST_GCC = @host_gcc@
HOST_STRIP = @host_strip@

HOST_AS = $(HOST_GCC) -c -x assembler-with-cpp
HOST_AS_CPP = $(HOST_GCC) -c -x assembler-with-cpp -P
# specifies c-specific -Ldirs
HOST_LD = splosion

# these are compilers for the host
BUILD_CC = cc
BUILD_GCC = gcc -m32

ifobjc
# currently, objc compilers are only used on NEXTSTEP builds.
# the native (build) NeXT c compiler can act as the native objc compiler,
# as well as the host objc compiler.
BUILD_OBJC = cc
HOST_OBJC = cc -arch @arch@
end ifobjc

DEFINES = @syn68k_define@ $(ROMLIB_DEFINES) \
  $(FRONT_END_DEFINES) $(HOST_ARCH_DEFINES) $(HOST_OS_DEFINES)

# include in -I$(HOST_..._DIR) also?
INCLUDES = -I. -I$(INCL_DIR) -I$(FRONT_END_DIR) -I$(HOST_OS_DIR) -I$(HOST_ARCH_DIR) -I$(SOUND_DIR)

# build cflags are those given when compiling for the build
# do not put optimized/debug flags here
BUILD_CFLAGS = $(CFLAGS) -DCOMPILE_FOR_BUILD $(DEFINES) $(INCLUDES)
# GEN_FLAGS is like BUILD_FLAGS, but used when building object files
# which include ROMlib header files but do not link against ROMlib
# (so ROMlib headers should define inline functions which reference
# external romlib functions).
# this is indicated by `-DNO_ROMLIB'
GEN_CFLAGS = $(BUILD_CFLAGS) -DNO_ROMLIB -Wall

ifobjc
BUILD_OBJCFLAGS = $(CFLAGS) $(DEFINES) $(INCLUDES)
end ifobjc

# cflags specified during configuration, defaults to `-g'
CONFIGURE_CFLAGS = @cflags@

# cflags for host
HOST_CFLAGS = $(DEFINES) $(INCLUDES) $(CONFIGURE_CFLAGS)

ifobjc
# `-traditional-cpp' is needed by NeXT cc to make sense of the
# global asm a5 decl
# HOST_OBJCFLAGS = -traditional-cpp $(HOST_CFLAGS)
# Haven't compiled NeXT based Executor in a very long time, but now that
# we have the beginnings of a Mac OS X port, don't mess it up by requesting
# -traditional-cpp.  If we ever do support NEXT hosts again it will be
# after moving to a more modern GNU build system.
HOST_OBJCFLAGS = $(HOST_CFLAGS)
end ifobjc

.SUFFIXES: .map .psw .h .c .m .s .scpp .o

# by default, .c files are built by the host gcc compiler,
# you must make an explicit rule to override this
.c.o: $<
	$(HOST_GCC) $(HOST_CFLAGS) -c -o $*.o $<

.m.o: $<
	$(HOST_OBJC) $(HOST_OBJCFLAGS) -c -o $*.o $<

.s.o: $<
	$(HOST_AS) -o $*.o $<

.scpp.o: $<
	$(HOST_AS_CPP) $(DEFINES) $(INCLUDES) -o $*.o $<

all:: executor

ifsyn68k
LIBSYN68K = -L$(SYN68K_LIB_DIR) -lsyn68k
end ifsyn68k

@host_arch_make@
@host_os_make@
@front_end_make@
@sound_make@
@executor_make@

ifeq ($(HAVE_IV), yes)
IV_SRC = iv-stubs.c
IV_OBJ = $(IV_SRC:.c=.o)

all:: iv-server
iv-server: iv-server.o
	$(HOST_GCC) -o iv-server iv-server.o $(IV_LIBS) $(FRONT_END_LIBS)

clean::
	rm -f iv-server iv-server.o $(IV_OBJ)
endif

HOST_OBJ = $(HOST_ARCH_OBJ) $(HOST_OS_OBJ) 
HOST_LIBS = $(HOST_ARCH_LIBS) $(HOST_OS_LIBS) 
HOST_LD_FLAGS = $(HOST_ARCH_LD_FLAGS) $(HOST_OS_LD_FLAGS)

executor: lowglobals.o $(ROMLIB_OBJ) $(EXECUTOR_OBJ) \
  $(HOST_OBJ) \
  $(FRONT_END_OBJ) $(IV_OBJ) $(SOUND_OBJ) $(HOST_OS_POST_LD_CMD)
	echo '#include "rsys/common.h"' > tmp-buildtime.c
	echo '#include "rsys/version.h"' >> tmp-buildtime.c
	echo 'const char ROMlib_executor_build_time[] = "'`date`'";' >> tmp-buildtime.c
	$(HOST_GCC) $(HOST_CFLAGS) -c -o buildtime.o tmp-buildtime.c
	rm -f tmp-buildtime.c
	@$(HOST_GCC) -o executor \
	  $(LD_FLAGS) $(HOST_LD_FLAGS) $(LOWGLOBALS_LD_OPTION) \
	  $(ROMLIB_OBJ) $(EXECUTOR_OBJ) buildtime.o \
	  $(HOST_OBJ) $(FRONT_END_OBJ) $(IV_OBJ) $(SOUND_OBJ) \
	  $(LIBSYN68K) $(FRONT_END_LIBS) $(HOST_LIBS) $(SOUND_LIBS)
	$(HOST_OS_POST_LD_CMD) $(HOST_OS_POST_LD_OPTIONS)

$(build_obj_dir_stamp):
	if [ ! -d $(build_obj_dir) ]; then \
	  mkdir $(build_obj_dir); \
	fi
	touch $(build_obj_dir_stamp)

clean::
	rm -f executor
	rm -rf $(build_obj_dir) buildtime.o

# We no longer use RCS; we use subversion, so we don't want any automatic
# checkouts
%:: RCS/%,v

include make.depend

depend-files: $(ROMLIB_SRC) $(EXECUTOR_SRC) $(HOST_SRC)		\
	      $(HOST_OS_SRC) $(FRONT_END_SRC) $(IV_SRC) $(SOUND_SRC)	\
	      $(MAP_C) $(EXECUTOR_GEN_C) $(HOST_ARCH_SRC) Makefile
	@echo $(filter %.c %.m,$^) > depend-files.tmp
	$(util_dir)/move-if-changed.sh depend-files.tmp depend-files

make.depend: depend-files
	-$(HOST_GCC) $(INCLUDES) -MM `cat depend-files` > make.depend

clean::
	rm -f depend-files make.depend
